

%macro prnt2(program=, disname=, ds_list=, vis=, popfl=);

%get_data(lst=adsu adsl,pre=,lib=adamw,supp=y);

proc freq data=adsu;
	tables sudosfrq PARAM*PARAMCD/ list;
run;

data adsu1;
	set adsu;
	where saffl='Y';
	if AVAL ne . then do;
		if sudosfrq='QD' then avaln=aval;
		else if sudosfrq='QM' then avaln=aval/30.4375;
		else if sudosfrq='PA' then avaln=aval/365.25;
	end;
	column=1;
	trtan=1;
	tocount=1;
	if adur=0 then adur=1/365.25;
	PARAM_ = strip(tranwrd(param,'(item)',''));
	if index(param_,"Cigar(s)" ) then param_="Cigar(s)";
	if index(param_,"Chewing Tobacco") then param_="Chewing Tobacco";
	if index(param_,"Electronic Cigarette(s)/E-Vapors") then param_="Electronic Cigarette(s)/E-Vapors";
	if index(param_,"Nicotine Product(s): (patch, gum, etc)") then param_="Nicotine Product(s): (patch, gum, etc)";
run;




proc sort data=adsu1;
	by param_ ;
run;
data adsu1;
set adsu1;
if ~missing(adur) then adur=floor(adur);
run;

proc sql;
	select count (distinct usubjid)
	into: n1 separated by ' '
	from adam.adsl 
	where saffl='Y'
	;
quit;

*******************************************************************************************************
Define specific formats for the report
******************************************************************************************************;
proc format;
 value level1l  1 = "Number of Tobacco Product Used Per Day"
                2 = "Duration of Tobacco Product Using(years)"
;
value level2l 1="n"
			  2="Mean (SD)"
			  3="Median"
			  4="Min, Max";
run;

*******************************************************************************************************
Data manipulation
******************************************************************************************************;



%univariate6(var=avaln, width=5,decimal=0,out=_tmp1,level1=1,level1FMT=level1l,level2=,level2FMT=level2l,level3=,wh=%str(avaln ne .),
			by= param_, in=adsu1);
%univariate6(var=adur, width=5,decimal=0,out=_tmp2,level1=2,level1FMT=level1l,level2=,level2FMT=level2l,level3=,wh=%str(adur ne .),
			by= param_, in=adsu1);

*******************************************************************************************************
Create report dataset
******************************************************************************************************;
data final;
set _tmp1 _tmp2 ; 
length txt $100.;
by level1 param_ level2;
txt = put(level1,level1l.);
where level2 ne 5;
page=level1;
if LEVEL2_LABEL="N" then LEVEL2_LABEL="n";
run;

proc sort data=final out=report;
by level1 param_ level2;
run;

proc sort data=report; by page level1 param_ level2; run;

proc sql;
 select count(distinct level2_label)
 into :nb_obse separated by ' '
 from report;
quit;
%put &nb_obse;

*******************************************************************************************************
Printing using proc report
******************************************************************************************************;
title3 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&disname..SU.rtf";

%open_rtf;
%empty_ds_fix(report);

title4 "Summary of Tobacco Use History";
title5 "(Safety Population)";

footnote3 j=l "Note: For current smokers, the end date used to calculate the duration of tobacco product using is the informed consent date.";
/*footnote4 j=l "         Estimated nicotine uptake = total cartridge weight change (mg) x nicotine concentration of the liquid (% by weight)";*/
/*footnote5 j=l " ";*/
/*footnote6 j=l " ";*/
/*footnote7 j=l " ";*/


proc report data=report nowd split='~' missing;
 column page level1 txt param_ level2 level2_label col1;
 define page         / " " order order=internal noprint;
 define level1       / " " order order=internal noprint;
 define level2       / "" order order=internal noprint;
 define txt / " " order group order=internal style(column)=[cellwidth=3.5in just=left] style(header)=[just=left];
 define param_ / "Tobacco Product" order order=internal style(column)=[cellwidth=3.5in just=left] style(header)=[just=left];
 
 define level2_label / " " order order=internal style(column)=[cellwidth=1.2in just=left] style(header)=[just=left];

 define col1 / " Overall~(N=&n1.)" style(column)=[cellwidth=1.6in just=c] style(header)=[just=c];
 
 break after page   / page;
 %if &nb_obse GT 0 %then %do;

 compute before param_ / style=[cellwidth=6in just=left];
  line " ";
 endcomp;
 %end;

 %if &nb_obse EQ 0 %then %do;
   compute after page/style=[cellwidth=9.7in just=left];
    line "No Values Were Measured.";
   endcomp;
 %end;

run;

ods rtf close;
ods listing;
%arrange_rtf;
%mend prnt2;

/*%prnt(program=T14_01_04_02_PROD_USE, disname=14.1.4.2, ds_list=ADEX, vis=%str((4 5 6), popfl=%str(SAFFL="Y"));*/
%prnt2(program=T14_01_03_SU, disname=14.1.3, ds_list=ADSU);

%clrw;
